<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <!-- 需要滚动的元素替代插槽 -->
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";
export default {
  name: "better-scroll",
  props: {
    probeType: {
      type: Number,
      default: 0,
    },
    pullUpLoad: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      scroll: null,
    };
  },
  mounted() {
    this.scroll = new BScroll(this.$refs.wrapper, {
      // 开启点击 使得滚动内部区域可以点击事件
      click: true,
      // 开启监听滚动位置
      probeType: this.probeType,
      // 开启上来加载更多
      // 是否开始执行监听滚动条是否到达底部
      pullUpLoad: this.pullUpLoad,
    });
    // 监听位置信息
    if(this.probeType === 2 || this.probeType === 3){
        this.scroll.on("scroll", (position) => {
        // console.log(position);
        this.$emit("scroll", position);
    });
    }
    // 滚动条到达底部会执行的函数
    // 当this.pullUpLoad为true时才开始监听是否滚动到底部 
    if(this.pullUpLoad) {

        this.scroll.on("pullingUp", () => {
    //  this.$emit("pullingUp");
        // console.log('到底部了');
        // 到底部了，就向外发送事件pullingUp =>home.vue中
        this.$emit('pullingUp')
    });
    }
  },
  methods: {
    scrollTo(x, y, time = 300) {
      this.scroll.scrollTo(x, y, time);
    },
    finishPullUp() {
      this.scroll.finishPullUp();
    },
    refresh(){
        this.scroll.refresh()
    }
  },
};
</script>
<style scoped>
</style>